package com.jiawa.wiki.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/*
     测试接口的耗时

 */
@Component
public class LogFilter  implements Filter {

    private static final Logger LOG = LoggerFactory.getLogger(LogFilter.class);


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //请求信息
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        LOG.info("------------LogFilter 开始--------------");
        LOG.info("请求地址:{} {}",request.getRequestURI().toString(),request.getMethod());
        LOG.info("远程地址{}" ,request.getRemoteAddr());
        long startTime = System.currentTimeMillis();
        filterChain.doFilter(servletRequest,servletResponse);// 如果没有其他的过滤器 就真正执行业务代码
        LOG.info("------------LogFilter 结束耗时：{}ms--------------",System.currentTimeMillis() - startTime);


    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}
